import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  
   	 state: {
   	         socketTask: null,
   	         websocketData: {}, // 存放从后端接收到的websocket数据
   				wsSynergyDate:{},//cunchu
   				wsAppliedDate:{},
   				wsAnnouncedDate:{},
   				wsSynergynum:0,
   				wsAppliednum:0,
   				wsAnnouncednum:0,
   				wsAllnum:0,
   				wsUrl:"",
   	     },
   		  
   	     mutations: {
   	         setWebsocketData (state, data) {
   	             state.websocketData = data
   					 if(state.websocketData.msgCategory==1){
   						 state.wsSynergyDate=state.websocketData;
   						 state.wsSynergynum+=1;
   						 uni.setStorage({
   						 	key: 'msgSynergykey',
   						 	data: state.wsSynergynum,
   						 	success: function () {
   						 	}
   						 });
   						 console.log("皆",state.wsSynergyDate);
   						 uni.getStorage({
   						 	key:'msgSynergykey',
   						 	success:(res)=>{
   						 		if(res.data){
   						 			state.wsSynergynum = res.data //定义全局变量供页面使用
   						 		}
   						 	}
   						 })
   					 };
   					
   					 
   					 if(state.websocketData.msgCategory==2){
   					 	state.wsAppliedDate=state.websocketData;
   						state.wsAppliednum+=1;
   						uni.setStorage({
   							key: 'msgAppliedkey',
   							data: state.wsAppliednum,
   							success: function () {
   							}
   						});
   						console.log("阵",state.wsAppliedDate);
   						uni.getStorage({
   							key:'msgAppliedkey',
   							success:(res)=>{
   								if(res.data){
   									state.wsAppliednum = res.data //定义全局变量供页面使用
   								}
   							}
   						})
   					 };
   					 
   					 
   					 if(state.websocketData.msgCategory==3){
   					 	state.wsAnnouncedDate=state.websocketData;
   						state.wsAnnouncednum+=1;
   						uni.setStorage({
   							key: 'msgAnnouncedkey',
   							data: state.wsAnnouncednum,
   							success: function () {
   							}
   						});
   						console.log("列",state.wsAnnouncedDate);
   						uni.getStorage({
   							key:'msgAnnouncedkey',
   							success:(res)=>{
   								if(res.data){
   									state.wsAnnouncednum = res.data //定义全局变量供页面使用
   								}
   							}
   						})
   					 };
   					state.wsAllnum=state.wsSynergynum+state.wsAppliednum+state.wsAnnouncednum;
   					 uni.setStorage({
   					 	key: 'msgAllkey',
   					 	data: state.wsAllnum,
   					 	success: function () {
   					 	}
   					 });
   					 uni.getStorage({
   					 	key:'msgAllkey',
   					 	success:(res)=>{
   					 		if(res.data){
   					 			state.wsAllnum = res.data //定义全局变量供页面使用
   					 		}
   					 	}
   					 });
   					 if(state.wsAllnum>0){
   						 uni.setTabBarBadge({
   							index: 0,
   							text:state.wsAllnum.toString(),
   						})	
   					 }else if(state.wsAllnum==0){
   						 uni.removeTabBarBadge({
   							 index: 0,
   						 })
   					 };
   					 
   	         }
   	     }, 
   		  
   		   actions: {
   				 websocketInit ({ state, dispatch }, url) {
   					  state.socketTast = uni.connectSocket({
   							url, // url是websocket连接ip
   							success: () => {
   								 console.log('websocket连接成功！')
   							},
   							fail: e => {
   								 console.log('连接失败' + e)
   							}
   					  })
   					  state.socketTast.onOpen(() => dispatch('websocketOnOpen'))
   					  state.socketTast.onMessage(res => dispatch('websocketOnMessage', res))
   					  state.socketTast.onClose(e => dispatch('websocketOnClose'))
   					  state.socketTast.onError(e => dispatch('websocketOnError'))
   				 },
   				 websocketOnOpen ({ commit }) {
   					  console.log('WebSocket连接正常打开中...！')
   				 },
   				  // 收到数据
   					websocketOnMessage ({ commit }, res) {
   						 console.log('收到服务器内容啦！！！：' + res.data)
   						 if (res.data !== 'success') {
   							  commit('setWebsocketData', (res && JSON.parse(res.data) || null))
   							  
   						 }
   					},
   					websocketOnClose ({ commit, dispatch }) {
   						 console.log('WebSocket连接关闭')
   					},
   					websocketOnError ({ commit, dispatch }) {
   						 console.log('WebSocket连接错误')
   					},
   					websocketClose ({ state }) {
   						 if (!state.socketTast) return
   						 state.socketTast.close({
   							  success (res) {
   									console.log('关闭成功', res)
   							  },
   							  fail (err) {
   									console.log('关闭失败', err)
   							  }
   						 })
   					},
   				  // 发送数据
   				  websocketSend ({ state }, data) {
   						uni.sendSocketMessage({
   							 data,
   							 success: res => {
   								  console.log('发送成功', res)
   							 },
   							 fail: e => {
   								  console.log('发送失败', e)
   							 }
   						})
   				  }
   			 },
   
   
})
export default store
